package Day10test;

import javax.swing.*;
import java.util.Scanner;
import java.util.regex.Pattern;

public class StudentUI {
    private Scanner sc = new Scanner(System.in);
    private SupperArray array = new SupperArray();
    private String genderParrtern = "^[男女]{1}$";
    private String pattern = "^[\u4e00-\u9fa5]{2,}$";

    /**
     * 菜单
     */
    public void menu(){
        JOptionPane.showMessageDialog(null,"欢迎使用学生管理系统");
        while(true){
            String input = JOptionPane.showInputDialog(null,"请选择：\n " +
                    "1-增加 2-修改 3-删除 4-查询 5-退出");
            if("1".equals(input)){
                insert();
            }else if("2".equals(input)){
                update();
            }else if("3".equals(input)){
                delete();
            }else if("4".equals(input)){
                search();
            }else if("5".equals(input)){
                JOptionPane.showMessageDialog(null,"再见");
                System.exit(0);
            }else{

            }
        }

    }

    /**
     *添加学生
     */
    public void insert() {

            String name = JOptionPane.showInputDialog(null, "请输入学生的姓名：");
                if(!name.matches(pattern)){
                    JOptionPane.showMessageDialog(null,"输入有误，姓名必须为两个以上的汉字");
                    return;
                }
        int age = 0;
        while (true){
            try {
                age = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入学生年龄："));
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "年龄输入出错");
                continue;
            }
            break;
        }
        String gender = JOptionPane.showInputDialog(null, "请输入学生性别");
        if(!gender.matches(genderParrtern)){
            JOptionPane.showMessageDialog(null,"性别只能为男或女");
            return;
        }
        int confirm = JOptionPane.showConfirmDialog(null,"确认添加该学生？");
        if(confirm == 0){
            array.add(new Student(name,age,gender));
            JOptionPane.showMessageDialog(null,"添加成功");
        }
    }
    /**
     *修改学生属性
     */
    public void update(){
        String name = JOptionPane.showInputDialog(null,"请输入要修改学生的姓名：");
        for(int i = 0;i < array.size();i++){
            if(array.get(i).equals(new Student(name))){
                String input = JOptionPane.showInputDialog(null,"请选择要修改的内容：1-姓名 2-年龄 3-性别");
                Student stu = (Student) array.get(i);
                if("1".equals(input)){
                    String newName = JOptionPane.showInputDialog(null,"请输入修改的新名字：");
                    if(!newName.matches(pattern)){
                        JOptionPane.showMessageDialog(null,"新名字必须为两个以上汉字");
                        return;
                    }
                    stu.setName(newName);
                }else if("2".equals(input)){
                   while (true){
                       try {
                           stu.setAge(Integer.parseInt(JOptionPane.showInputDialog(null,"请输入修改的新年龄：")));
                       }catch (NumberFormatException e){
                           JOptionPane.showMessageDialog(null, "年龄输入出错");
                           continue;
                       }
                       break;
                   }
                }else if("3".equals(input)){
                    String newGender = JOptionPane.showInputDialog(null,"请输入修改的新性别：");
                    if(!newGender.matches(genderParrtern)){
                        JOptionPane.showMessageDialog(null,"性别必须为男女");
                        return;
                    }
                    stu.setGender(newGender);
                }else{
                    JOptionPane.showMessageDialog(null,"输入有误");
                    return;
                }
                JOptionPane.showMessageDialog(null,"修改完成");
                return;
            }
        }
        JOptionPane.showMessageDialog(null,"查无此人");
    }
    /**
     *删除学生
     */
    public void delete(){
        String name = JOptionPane.showInputDialog(null,"请输入要删除学生的姓名：");
        if (name == null){
            return;
        }
        for(int i = 0;i < array.size();i++){
            if(array.get(i).equals(new Student(name))){
                array.remove(i);
                JOptionPane.showMessageDialog(null,"删除成功");
                return;
            }
        }
        JOptionPane.showMessageDialog(null,"查无此人");
    }
    /**
     *查找学生
     */
    public void search(){
        String input = JOptionPane.showInputDialog(null,"请选择：\n 1-查询所有 2-学生姓名 3-模糊搜索");
        if("1".equals(input)){
            String str = "";
            for(int i = 0;i < array.size();i++){
                str += array.get(i) + "\n";
            }
            JOptionPane.showMessageDialog(null,str);
        }else{
            String name = JOptionPane.showInputDialog(null,"请输入要查询的学生姓名：");
            if (name == null){
                return;
            }
            boolean flag = false;
            for(int i = 0;i < array.size();i++){
                if(array.get(i).toString().matches("^.*"+name+".*$")){
                    JOptionPane.showMessageDialog(null,array.get(i));
                    flag = true;
                }
            }
            if(!flag){
                JOptionPane.showMessageDialog(null,"查无此人");
            }
        }
    }
}
